/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers.Beans;

import Entities.Rol;
import Entities.Student;
import Entities.User;
import Facades.RolFacade;
import Facades.StudentFacade;
import Facades.UserFacade;
import com.sun.tools.doclets.internal.toolkit.SerializedFormWriter;
import java.io.Serializable;
import java.util.Date;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author roycalderon
 */
@Named(value = "registerBean")
@SessionScoped
public class RegisterBean implements Serializable {

    public RegisterBean() {
    }
    //Var usadas
    private String userName;
    private String passWord;
    private String passWord2;
    private String fullName;
    private String email;
    private String idNumber;
    //Para el Growwwwl
    private ExternalContext ex = FacesContext.getCurrentInstance().getExternalContext();
    //Entidades
    private User entity_User;
    private Student entity_Student;
    private Rol entity_Rol;
    //Facades
    @EJB
    private UserFacade ejbFacade_User;
    @EJB
    private StudentFacade ejbFacade_Student;
    @EJB
    private RolFacade ejbFacade_Rol;

    //Metodo de registro Principal
    public String register() {
        if (!userName.isEmpty() & !passWord.isEmpty() & !passWord2.isEmpty() & !fullName.isEmpty() & !email.isEmpty() & !idNumber.isEmpty()) {
            //Datos ingresados correctamente
            if (passWord.equals(passWord2)) {
                //Verifico que el usuario no se haya creado previamente
                entity_User = ejbFacade_User.FindbyUser(userName);
                if (entity_User == null) {
                    //Verifico que el Estudiante no se haya creado previamente
                    entity_Student = ejbFacade_Student.FindbyidNumberStudent(idNumber);
                    if (entity_Student == null) {
                        //Luz Verde para crear el usuario...
                        //Instancio el objeto de Usuario
                        if (entity_User == null) {
                            entity_User = new User();
                        }
                        entity_User.setUserName(userName);
                        entity_User.setPassWord(passWord);
                        entity_User.setRegisterDate(new Date());
                        entity_User.setLastLoginDate(new Date());
                        entity_User.setPicture("default.png");
                        entity_User.setEmail(email);
                        //Finding Student Rol  
                        entity_Rol = ejbFacade_Rol.findByRolidRol(2);
                        entity_User.setRolidRol(entity_Rol);

                        ejbFacade_User.create(entity_User);

                        if (entity_Student == null) {
                            entity_Student= new Student();
                        }
                        entity_Student.setIdStudent(Integer.parseInt(idNumber));
                        entity_Student.setFullName(fullName);
                        entity_Student.setIdNumber(idNumber);
                        entity_Student.setStatus(true);
                        entity_Student.setUserid(entity_User);
                        entity_Student.setActivationDate(null);

                        ejbFacade_Student.create(entity_Student);
                        //Estudiante Creado exitosamente
                        FacesContext.getCurrentInstance().addMessage("Register", new FacesMessage(FacesMessage.SEVERITY_INFO, "Estudiante Creado exitosamente!!!! :)", "detail"));
                        return "index.xhtml";
                    } else {
                        //Ya existe el Studiante
                        FacesContext.getCurrentInstance().addMessage("Register", new FacesMessage(FacesMessage.SEVERITY_INFO, "Ya existe el Estudiante", "detail"));
                        return "register.xhtml";
                    }
                } else {
                    //Ya existe el usuario...
                    FacesContext.getCurrentInstance().addMessage("Register", new FacesMessage(FacesMessage.SEVERITY_INFO, "Ya existe el usuario", "detail"));
                    return "register.xhtml";
                }
            } else {
                //Mensaje de Claves no coinciden
                FacesContext.getCurrentInstance().addMessage("Register", new FacesMessage(ResourceBundle.getBundle("/Bundle").getString("InvalidUserandPassword")));
                return "register.xhtml";
            }
        } else {
            //Mensaje de Campos Vacios
            FacesContext.getCurrentInstance().addMessage("Login", new FacesMessage(ResourceBundle.getBundle("/Bundle").getString("TypeUserandPassword")));
            return null;

        }
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getPassWord2() {
        return passWord2;
    }

    public void setPassWord2(String passWord2) {
        this.passWord2 = passWord2;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getIdNumber() {
        return idNumber;
    }

    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }
}
